package textprocessing.nlpanalysis.constituent;

import algorithm.config.HumanLanguage;
import algorithm.nlp.corenlp.TextAnnotator;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.util.CoreMap;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by julianzliu on 4/19/2017.
 */
public class ContextGenerator {


    private Recombinator recombination;
    private SCITreeGenerator generator;


    public ContextGenerator(){
        //this.textAnnotator = annotator;
        //this.document = this.textAnnotator.getAnnotatedText( text );
        this.generator = new SCITreeGenerator();
        this.recombination = new Recombinator();
    }


    public List<ContextItem> generateContextsOfDocument(Annotation document){
        List<ContextItem> docContexts = new ArrayList<>();
        List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
        for(CoreMap sentence: sentences){
            List<ContextItem> senContexts = generateContextsOfSentence(sentence);
            docContexts.addAll(senContexts);
        }
        return docContexts;
    }


    public List<ContextItem> generateContextsOfSentence(CoreMap sentence){
        /*
        1) 一个句子生成多个 SCI Tree
        2) 一个 SCI Tree 生成多个 Context
         */
        List<ContextItem> allContexts = new ArrayList<>();
        /*
        1) Generate SCI Tree
         */
        List<SCITree> sciTrees = generator.getSCITreeOfSentence(sentence);

        /*
        2) Recombinator
         */
        for(SCITree tree: sciTrees){
            System.out.println("\n\n" + "---------------------" + " label tree " + "--------------------");
            SCITree.traversal_print(tree.root, "");


            // 每棵树生成多个Context
            List<ContextItem> contextItems = recombination.generate_contexts(tree);

            System.out.println("\n\n" + "---------------------" + " new tree " + "--------------------");
            SCITree.traversal_print(tree.root, "");

            for(ContextItem item: contextItems){
                //System.out.println( item.toString() );
                allContexts.add(item);
            }
        }
        return allContexts;
    }


    /*************************************************************************************************************************
     *
     */
    public static void main(String[] args){
        String text = "apple and banana are both fruit.";
        text = "Barack Obama, who is the president of USA, was born in Hawaii, however his father was born in Kenya and his mother was born in Kansas.";
        if(args.length > 0){
            text = "";
            for(String arg: args){
                text += " " + arg;
            }
        }

        TextAnnotator textAnnotator = new TextAnnotator(HumanLanguage.ENGLISH);
        Annotation document = textAnnotator.getAnnotatedText(text);

        ContextGenerator generator = new ContextGenerator();

        List<ContextItem> contextItemList = generator.generateContextsOfDocument(document);

        System.out.println("\n\n\n\n-------------" + "Print Context List" + "-------------------");
        for(ContextItem item: contextItemList){
            System.out.println(item.toString());
        }

    }

}
